package news.servlet;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

public class BaseServlet extends HttpServlet {
    private HttpServletRequest req = null;
    private HttpServletResponse resp = null;

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        this.req = req;
        this.resp = resp;
        super.service(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.getWriter().println("not implement");
    }

    protected void showPage(String filename) throws ServletException, IOException {
        this.req.getRequestDispatcher("/WEB-INF/views/" + filename + ".jsp")
                .forward(this.req, this.resp);
    }

    protected int getIntegerParam(String name,int defaultValue){
        String value = this.req.getParameter(name);//获取到参数
        // 判断参数是否是数字
        if(value == null || value.length() == 0 || !value.matches("^\\d+$")){
            return defaultValue;
        }
        return Integer.parseInt(value);
    }
}
